/**
ProductQuotasZTreeFunction.java
business
2012-8-10上午9:21:39
@author 屈甲康
@QQ285799123
mailto:qujiakang@126.com
**/
package org.jackysoft.report.google.function;

import java.util.Collection;

import javax.annotation.Nullable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jackysoft.entity.Department;
import org.jackysoft.entity.Product;
import org.jackysoft.entity.ProductQuota;
import org.jackysoft.google.collection.AbstractZTreeFunction;
import org.jackysoft.google.collection.ZTreeNode;
import org.jackysoft.service.ProductService;
import org.springframework.util.CollectionUtils;

public class ProductQuotasZTreeFunction extends
		AbstractZTreeFunction<ProductQuota, ZTreeNode> {

	static final Log log = LogFactory.getLog(ProductQuotasZTreeFunction.class);
	private ProductService productService;
		
	public ProductQuotasZTreeFunction(ProductService productService) {
		super();
		this.productService = productService;
	}



	@Override
	public ZTreeNode apply(@Nullable ProductQuota from) {
		
		ZTreeNode zt = new ZTreeNode();
		Product owner = from.getOwner();
		Collection<ProductQuota>  quotas = productService.getsQuotas(owner);
		//log.info("产品 "+owner.getCode()+" 定额数 ："+quotas.size());
		boolean isleaf = CollectionUtils.isEmpty(quotas);
		Product host = from.getHost();
		Collection<Department> pvds = owner.getProviders();
		String depts = "";
		for(Department d : pvds)depts+=d.getName()+",";
		depts=("".equals(depts)?"":depts.substring(0, depts.lastIndexOf(',')));
		zt.setId(owner.getCode()+"|"+host.getCode());
		zt.setKey(owner.getCode());
		zt.setName(owner.getCode()+"["+owner.getName()+"]-["+from.getConsume()+"]-["+(depts)+"]");
		zt.setLeaf(isleaf);
		zt.setIsParent(isleaf?"false":"true");
	    zt.attr("code", owner.getCode())
	      .attr("hostcode", host.getCode())
	      .attr("ownercode", owner.getCode())	      
	      .attr("consume", from.getConsume())
	      .attr("onquota", true);	    
		return zt;
	}

}
